package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**

 * <p>

 * 课程视频 前端控制器

 * </p>
 *

 * @author testjava

 * @since 2020-03-02
 */
@RestController
@Api(description = "小结管理")
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    //注入vodclient
    @Autowired
    private VodClient vodClient;

    @ApiOperation(value = "添加小结")
    //添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo) {
        videoService.save(eduVideo);
        return R.ok();
    }

    @ApiOperation(value = "删除小结")
    //删除小节，删除阿里云中的对应视频
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id) {
        //根据小结id得到视频id
        //获得小结id
        EduVideo eduVideo = videoService.getById(id);
        //获得视频id
        String videoSourceId = eduVideo.getVideoSourceId();

        //判断小结里面是否有视频id
        if (!StringUtils.isEmpty(videoSourceId)){
            //根据视频id,远程调用实现视频删除
            R result = vodClient.removeAlyVideo(videoSourceId);
            if (result.getCode() == 20001){
                throw  new GuliException(20001,"删除视频失败，熔断器。。。。");
            }
        }
        //删除小结
        videoService.removeById(id);
        return R.ok();
    }

    //修改小节 TODO
    @ApiOperation(value = "根据id查询小结")
    //根据章节id查询
    @GetMapping("getVideoInfo/{VideoId}")
    public R getVideoInfo(@PathVariable String VideoId) {
        EduVideo eduVideo = videoService.getById(VideoId);
        return R.ok().data("eduVideo",eduVideo);
    }

    @ApiOperation(value = "修改章节")
    //修改章节
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo) {
        videoService.updateById(eduVideo);
        return R.ok();
    }

}

